using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace TuMama
{
    public abstract class ByteMp3
    {

        public static byte[] LeerDeArchivo(string path, string fileName, string extension)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(path);
                if (di.Exists == false)
                {
                    di.Create();
                }
                string file = Path.ChangeExtension(fileName, extension);
                string filePath = Path.Combine(path, file);
                using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read))
                {
                    byte[] bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, bytes.Length);
                    return bytes;
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException("No se pudo leer el archivo", e);
            }
        }

        public static byte[] LeerDeArchivo(string fileName)
        {
            String ruta = Environment.CurrentDirectory;
            String path = Path.Combine(ruta, "Music");
            return ByteMp3.LeerDeArchivo(path, fileName, "mp3");
        }

        public static string ConvertirHaciaArchivo(Byte[] content, string path, string fileName, string extension)
        {

            try
            {
                string file = Path.ChangeExtension(fileName, extension);
                DirectoryInfo di = new DirectoryInfo(path);
                if (di.Exists == false)
                {
                    di.Create();
                }
                using (FileStream fs = new FileStream(Path.Combine(path, file), FileMode.Create))
                {
                    fs.Write(content, 0, System.Convert.ToInt32(content.Length));
                    fs.Seek(0, SeekOrigin.Begin);
                    fs.Close();
                }
                return Path.Combine(path,file);
            }
            catch (Exception e)
            {
                throw new ApplicationException("No se pudo convertir el archivo", e);
            }
        }

        public static string ConvertirHaciaArchivo(Byte[] content, string fileName)
        {
            String ruta = Environment.CurrentDirectory;
            String path = Path.Combine(ruta, "Music");
            return ConvertirHaciaArchivo(content, path, fileName, "mp3");
        }
    }
}
